#include <reg52.h>
#include <intrins.h>


sbit adrd = P3^7;
sbit adwr = P3^6;
sbit wela = P2^7;
sbit dula = P2^6;

unsigned char code_table[] = {
	0x3F, 0x06, 0x5B,
	0x4F, 0x66, 0x6D,
	0x7D, 0x07, 0x7F,
	0x6F, 0x77, 0x7C,
	0x39, 0x58, 0x5E,
	0x79};

void delay(int ms){
	int i, j;
	for(i = ms; i > 0; i--)
		for(j = 110; j > 0; j--);
}

void display(unsigned int b, unsigned s, unsigned g){
	dula = 1;
	P0 = code_table[b];
	dula = 0;
	P0 = 0xff;
	wela = 1;
	P0 = 0x7e;
	wela = 0;
	delay(5);
	
	dula = 1;
	P0 = code_table[s];
	dula = 0;
	P0 = 0xff;
	wela = 1;
	P0 = 0x7d;
	wela = 0;
	delay(5);
	
	dula = 1;
	P0 = code_table[g];
	dula = 0;
	P0 = 0xff;
	wela = 1;
	P0 = 0x7b;
	wela = 0;
	delay(5);
	
}

void main(){
    unsigned int a, A1, A2, A3, adval;
    wela = 1;
    P0 = 0x7f;
    wela = 0;

    while(1){
        adwr = 1;
        _nop_();
        adwr = 0;
        _nop_();
        adwr = 1;
        for (a = 0; a < 50; a++){
            display(A1, A2, A3);
            
        }
        P1 = 0xff;
        adrd = 1;
        _nop_();
        adrd = 0;
        _nop_();
        adval = P1;
		adrd = 1;
        A1 = adval / 100;
        A2 = adval % 100 / 10;
        A3 = adval % 10;
    } 
	IIC_sentByte(0xf4);
}